
#import "ActivityView.h"

@implementation ActivityView

//
// singleton
//
static ActivityView* globalView												= nil;

//
// original view
//
@synthesize originalView													= _originalView;

//
// border view
//
@synthesize borderView														= _borderView;

//
// activity indicator
//
@synthesize activityIndicatorView											= _activityIndicatorView;

//
// label
//
@synthesize textLabel														= _textLabel;

//
// show network activity indicator
//
@synthesize showNetworkActivityIndicator									= _showNetworkActivityIndicator;

//
// current view
//
+ (ActivityView*)currentView
{
	return globalView;
}

//
// remove view
//
+ (void)removeView
{
	if(!globalView)
		return;
	
	if(globalView.showNetworkActivityIndicator)
		[UIApplication sharedApplication].networkActivityIndicatorVisible	= NO;
	
	[globalView removeFromSuperview];
	[globalView release];
	globalView																= nil;
}

//
// keyboard view
//
+ (UIView*)keyboardView
{
	NSArray* windowList														= [[UIApplication sharedApplication] windows];
	for(UIWindow* theWindow in [windowList reverseObjectEnumerator])
	{
		for(UIView* theView in [theWindow subviews])
		{
			if(!strcmp(object_getClassName(theView), "UIPeripheralHostView") || !strcmp(object_getClassName(theView), "UIKeyboard"))
			{
				return theView;
			}
		}
	}
	
	return nil;
}

//
// hack for keyboard
//
- (UIView*)hackViewForKeyboard:(UIView*)theView
{
	UIView* keyboardView													= [[self class] keyboardView];
	return keyboardView ? keyboardView.superview : theView;
}

//
// setup background
//
- (void)setupBackground
{
	self.opaque																= NO;
	self.autoresizingMask													= UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
	self.backgroundColor													= [[UIColor blackColor] colorWithAlphaComponent:0.35];
	
	if(!self.borderView)
	{
		self.borderView														= [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
		self.borderView.opaque												= NO;
		self.borderView.autoresizingMask									= UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
		self.borderView.backgroundColor										= [[UIColor blackColor] colorWithAlphaComponent:0.5];
		self.borderView.layer.cornerRadius									= 10.0;
	}
	
	if(!self.activityIndicatorView)
	{
		self.activityIndicatorView											= [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge] autorelease];
		[self.activityIndicatorView startAnimating];
	}
	
	if(!self.textLabel)
	{
		self.textLabel														= [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
		self.textLabel.font													= [UIFont boldSystemFontOfSize:[UIFont systemFontSize]];
		self.textLabel.textAlignment										= UITextAlignmentCenter;
		self.textLabel.textColor											= [UIColor whiteColor];
		self.textLabel.backgroundColor										= [UIColor clearColor];
		self.textLabel.numberOfLines										= 0;
		self.textLabel.lineBreakMode										= UILineBreakModeWordWrap;
	}
}

//
// init
//
- (id)initWithView:(UIView*)theView labelText:(NSString*)labelText
{
	//
	// call super
	//
	self																	= [super initWithFrame:CGRectZero];
	if(!self)
		return nil;
	
	//
	// remove existing view
	//
	if(globalView)
		[[self class] removeView];
	
	//
	// save singleton
	//
	globalView																= self;
	
	//
	// save parent view
	//
	self.originalView														= theView;
	theView																	= [self hackViewForKeyboard:theView];
	
	//
	// setup background
	//
	[self setupBackground];
	self.textLabel.text														= labelText;
	
	//
	// setup views
	//
	[theView addSubview:self];
	[self addSubview:self.borderView];
	[self.borderView addSubview:self.activityIndicatorView];
	[self.borderView addSubview:self.textLabel];
	
	return self;
}

//
// new view
//
+ (id)newActivityViewWithView:(UIView*)theView labelText:(NSString*)labelText
{
	return [[self alloc] initWithView:theView labelText:labelText];
}

//
// dealloc
//
- (void)dealloc
{
	self.textLabel															= nil;
	self.activityIndicatorView												= nil;
	self.borderView															= nil;
	self.originalView														= nil;
	
	[super dealloc];
}

//
// show network activity indicator
//
- (void)setShowNetworkActivityIndicator:(BOOL)showNetworkActivityIndicator
{
	_showNetworkActivityIndicator											= showNetworkActivityIndicator;
	[UIApplication sharedApplication].networkActivityIndicatorVisible		= showNetworkActivityIndicator;
}

//
// layout subviews
//
- (void)layoutSubviews
{
	//
	// animating
	//
	if(!CGAffineTransformIsIdentity(self.borderView.transform))
		return;
	
	//
	// calc size
	//
	self.frame																= self.superview != self.originalView ? [self.originalView convertRect:self.originalView.bounds toView:self.superview] : self.superview.bounds;
	CGSize maxSize															= CGSizeMake(260, 400);
	CGSize textSize															= [self.textLabel.text sizeWithFont:[UIFont boldSystemFontOfSize:[UIFont systemFontSize]] constrainedToSize:maxSize lineBreakMode:self.textLabel.lineBreakMode];
	if(textSize.width < self.activityIndicatorView.frame.size.width)
		textSize.width														= self.activityIndicatorView.frame.size.width + 10;
	
	//
	// cacl border frame size
	//
	self.textLabel.frame													= CGRectMake(self.textLabel.frame.origin.x, self.textLabel.frame.origin.y, textSize.width, textSize.height);
	CGRect borderFrame														= CGRectZero;
	borderFrame.size.width													= textSize.width + 30;
	borderFrame.size.height													= self.activityIndicatorView.frame.size.height + textSize.height + 40;
	borderFrame.origin.x													= floor((self.frame.size.width - borderFrame.size.width) / 2);
	borderFrame.origin.y													= floor((self.frame.size.height - borderFrame.size.height) / 2);
	self.borderView.frame													= borderFrame;
	
	//
	// calc indicator frame size
	//
	CGRect indicatorFrame													= self.activityIndicatorView.frame;
	indicatorFrame.origin.x													= (borderFrame.size.width - indicatorFrame.size.width) / 2;
	indicatorFrame.origin.y													= 20;
	self.activityIndicatorView.frame										= indicatorFrame;
	
	//
	// calc label frame size
	//
	CGRect labelFrame														= self.textLabel.frame;
	labelFrame.origin.x														= floor((borderFrame.size.width - labelFrame.size.width) / 2);
	labelFrame.origin.y														= borderFrame.size.height - labelFrame.size.height - 10;
	self.textLabel.frame													= labelFrame;
}

//
// update text
//
- (void)updateText:(NSString*)labelText
{
	self.textLabel.text														= labelText;
	[self layoutSubviews];
}

//
// show activity view
//
+ (ActivityView*)showActivityView:(NSString*)labelText networking:(BOOL)networking
{
	UIWindow* window														= [[[UIApplication sharedApplication] windows] objectAtIndex:0];
	UIView* rootView														= window.rootViewController.view;
	return [self showActivityView:labelText networking:networking withView:rootView];
}

//
// show activity view
//
+ (ActivityView*)showActivityView:(NSString*)labelText networking:(BOOL)networking withView:(UIView*)theView
{
	ActivityView* activityView												= [self newActivityViewWithView:theView labelText:labelText];
	activityView.showNetworkActivityIndicator								= networking;
	return activityView;
}

@end
